package cn.chen.miaosha.controller;

import cn.chen.miaosha.rabbitmq.MQSender;
import cn.chen.miaosha.redis.RedisService;
import cn.chen.miaosha.redis.UserKey;
import cn.chen.miaosha.domain.User;
import cn.chen.miaosha.result.Result;
import cn.chen.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class SampleController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    MQSender sender;

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name", "chenxiaokang");
        return "hello";
    }

    //swagger
//    @RequestMapping("/mq/fanout")
//    @ResponseBody
//    public Result<String> fanout(){
//        sender.sendFanout("hello,fanout");
//        return new Result<String>("hello fanout");
//    }
//
//    @RequestMapping("/mq/header")
//    @ResponseBody
//    public Result<String> headers(){
//        sender.sendFanout("hello,headers");
//        return new Result<String>("hello headers");
//    }
//
//    @RequestMapping("/mq")
//    @ResponseBody
//    public Result<String> mq(){
//        sender.send("hello,mq");
//        return new Result<String>("hello world");
//    }
//
//    @RequestMapping("/mq/topic")
//    @ResponseBody
//    public Result<String> mqTopic(){
//        sender.sendTopic("hello,topicMq");
//        return new Result<String>("hello topic");
//    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet(){
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet(){
        User user = redisService.get(UserKey.getById,""+1,User.class);
        return Result.success(user);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Boolean> redisSet(){
        User user = new User(1,"1111");
        redisService.set(UserKey.getById, ""+1, user);
        return Result.success(true);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx(){
        userService.tx();
        return Result.success(true);
    }

}
